﻿var contador = 0;
var Punto = "false";
var PuntoOld = "false";
function initializeMap() {
    
    var mapOptions = {
        zoom: 16,
        };
        map = new google.maps.Map(document.getElementById('map-canvas'),
            mapOptions);    
}

function locError(error) {
    // the current position could not be located
    alert("The current position could not be found!");
}

function setCurrentPosition(pos) {
    currentPositionMarker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(
            pos.coords.latitude,
            pos.coords.longitude
        ),
        title: "Current Position",
        icon: "Recursos/bus.png"
    });
    map.panTo(new google.maps.LatLng(
            pos.coords.latitude,
            pos.coords.longitude
        ));
}

function displayAndWatch(position) {
    // set current position
    setCurrentPosition(position);
    // watch position
    watchCurrentPosition();
}

function watchCurrentPosition() {
    var positionTimer = navigator.geolocation.watchPosition(
        function (position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var accuracy = position.coords.accuracy;
            var velocidad = parseInt((position.coords.speed)*3.6);
            var capa = document.getElementById("capa");
            Punto = CompararPuntosControl(latitude, longitude);
            if (accuracy <= 30) {
                if (Punto == PuntoOld) {
                    Punto = "false";
                }
                else {
                    if ((Punto != "false") && (Punto != PuntoOld)) {
                        PuntoOld = Punto;
                    } else {
                        if ((Punto == "false") && (PuntoOld == "DESPACHO")) {
                            Punto = "SALIO DESPACHO";
                            PuntoOld = "false";
                        }
                    }
                }

                ModCoordenadas(latitude, longitude, velocidad, Punto);
            }
            setMarkerPosition(currentPositionMarker, position);
            capa.innerHTML = "latitud: " + latitude + " longitud: " + longitude + "Velocidad: " + velocidad + " Punto: " + Punto + " PuntoOld: " + PuntoOld;
        }, function error(msg) {
            alert('Please enable your GPS position future.');
        }, { maximumAge: 0, timeout: 3000, enableHighAccuracy: true });
}

function CompararPuntosControl(latitude, longitude) {
    var Respuesta = "false";
    if ((latitude <= 10.464524093693147 + 0.0005) && (latitude >= 10.464524093693147 - 0.0005)) {
        if ((longitude <= -73.2490935886749 + 0.0005) && (longitude >= -73.2490935886749 - 0.0005)) {
            Respuesta = "DESPACHO";
        }
    }
    if ((latitude <= 10.478587443666585 + 0.001) && (latitude >= 10.478587443666585 - 0.001)) {
        if ((longitude <= -73.24457674869382 + 0.001) && (longitude >= -73.24457674869382 - 0.001)) {
            Respuesta = "TECNOPARQUE";
        }
    }
    if ((latitude <= 10.449943113207558 + 0.001) && (latitude >= 10.449943113207558 - 0.001)) {
        if ((longitude <= -73.26027303584897 + 0.001) && (longitude >= -73.26027303584897 - 0.001)) {
            Respuesta = "UNICESAR";
        }
    }
    if ((latitude <= 10.4454378527192 + 0.001) && (latitude >= 10.4454378527192 - 0.001)) {
        if ((longitude <= -73.24261337169492 + 0.001) && (longitude >= -73.24261337169492 - 0.001)) {
            Respuesta = "TERMINAL";
        }
    }
    if ((latitude <= 10.473122541751476 + 0.001) && (latitude >= 10.473122541751476 - 0.001)) {
        if ((longitude <= -73.24366479762875 + 0.001) && (longitude >= -73.24366479762875 - 0.001)) {
            Respuesta = "GALERIA";
        }
    }
    if ((latitude <= 10.47375554690831 + 0.001) && (latitude >= 10.47375554690831 - 0.001)) {
        if ((longitude <= -73.24915796169125 + 0.001) && (longitude >= -73.24915796169125 - 0.001)) {
            Respuesta = "LOPERENA CENTRAL";
        }
    }
    if ((latitude <= 10.456041464772705 + 0.001) && (latitude >= 10.456041464772705 - 0.001)) {
        if ((longitude <= -73.242988880957 + 0.001) && (longitude >= -73.242988880957 - 0.001)) {
            Respuesta = "SEMAFORO LA 30";
        }
    }
    if ((latitude <= 10.462524788483401 + 0.001) && (latitude >= 10.462524788483401 - 0.001)) {
        if ((longitude <= -73.23028593906247 + 0.001) && (longitude >= -73.23028593906247 - 0.001)) {
            Respuesta = "CR 4 Cl 28";
        }
    }
    if ((latitude <= 10.471977854143676 + 0.001) && (latitude >= 10.471977854143676 - 0.001)) {
        if ((longitude <= -73.24693709262692 + 0.001) && (longitude >= -73.24693709262692 - 0.001)) {
            Respuesta = "CL 17 CR 11";
        }
    }
    return Respuesta;
}

function setMarkerPosition(marker, position) {
    marker.setPosition(
        new google.maps.LatLng(
            position.coords.latitude,
            position.coords.longitude)
    );
}

function initLocationProcedure() {
    initializeMap();
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
    } else {
        alert("Your browser does not support the Geolocation API");
    }
}

function ModCoordenadas(latitud1, longitud1, velocidad, Punto) {
    var BusAct = localStorage.getItem("actBus");
    var parametro = {
        plac: "'" + BusAct + "'",
        latitud: "'" + latitud1 + "'",
        longitud: "'" + longitud1 + "'",
        Velocidad: "'" + velocidad + "'",
        Punto: "'" + Punto + "'"
    };
    var DirSer = localStorage.getItem("DirSer");
    var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/EnviarCoordenadas";
    $.ajax({
        type: "GET",
        url: ruta,
        data: parametro,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
        },
        error: function (jqXHR, status, error) {
        }
    });
}